package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.LotTogetherAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.LotLists;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class LotTogetherActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etTogetherLot)
    FreeUI_ClearEditText etTogetherLot;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    private LotTogetherAdapter lotTogetherAdapter;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lottogether;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvBaseSubmit.setVisibility(View.GONE);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);

        lotTogetherAdapter = new LotTogetherAdapter(null);
        lotTogetherAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        lotTogetherAdapter.isFirstOnly(true);
        initRecyclerView(rvLotList, this);
        rvLotList.setAdapter(lotTogetherAdapter);
        initEdit(etLot, s->{

        });
        initEdit(etTogetherLot, s->{

            if (s.equals(getText(etLot))){
                FreeApi_ToastUtils.showFreeToast("主批次与合批批次不能相同", LotTogetherActivity.this, false, 0);
                return;
            }

            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), LotTogetherActivity.this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(LotTogetherActivity.this, false, getSupportFragmentManager())) {
                        FreeApi_ToastUtils.showFreeToast("合批成功", LotTogetherActivity.this, true, 0);
                        lotTogetherAdapter.addData(0,s);
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "LOTMERGEORUNMERGE");
                    FreeApi_ToastUtils.showFreeToast("连接服务器失败", LotTogetherActivity.this, false, 0);
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "LotMerge");
            map.addProperty("PARENTLOTID", getText(etLot));
            map.addProperty("ACTIONCOMMENT", "");
            List<LotLists> lotList = new ArrayList<>();
            LotLists lotList1 = new LotLists(s);
            lotList.add(lotList1);
            map.add("CHILDLOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
            NetUtils.requestPostNet("IF.LOTMERGEORUNMERGE", "Common", LotTogetherActivity.this, map, callBack);

        });
    }

    @Override
    protected void initData() {

    }
}
